home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zpacked.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  195 lines

  1. /* Copyright (C) 1990, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zpacked.c */
  20. /* Packed array operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "alloc.h"
  24. #include "dict.h"
  25. #include "iname.h"
  26. #include "ivmspace.h"
  27. #include "oper.h"
  28. #include "packed.h"
  29. #include "save.h"            /* for alloc_refs */
  30. #include "store.h"
  31.  
  32. /* Import the array packing flag */
  33. extern ref ref_array_packing;
  34.  
  35. /* - currentpacking <bool> */
  36. int
  37. zcurrentpacking(register os_ptr op)
  38. {    push(1);
  39.     ref_assign(op, &ref_array_packing);
  40.     return 0;
  41. }
  42.  
  43. /* <obj_0> ... <obj_n-1> <n> packedarray <packedarray> */
  44. int
  45. zpackedarray(register os_ptr op)
  46. {    int code;
  47.     uint size;
  48.     os_ptr aop;
  49.     check_type(*op, t_integer);
  50.     if ( op->value.intval < 0 ||
  51.          op->value.intval > max_uint / sizeof(ref) - 1
  52.        )
  53.         return_error(e_rangecheck);
  54.     size = op->value.intval;
  55.     if ( size > op - osbot )
  56.         return_error(e_stackunderflow);
  57.     aop = op - size;
  58.     code = make_packed_array(aop, size, aop, "packedarray");
  59.     if ( code >= 0 )
  60.        {    pop(size);
  61.        }
  62.     return code;
  63. }
  64.  
  65. /* <bool> setpacking - */
  66. int
  67. zsetpacking(register os_ptr op)
  68. {    check_type(*op, t_boolean);
  69.     ref_assign_old(&ref_array_packing, op, "setpacking");
  70.     pop(1);
  71.     return 0;
  72. }
  73.  
  74. /* ------ Non-operator routines ------ */
  75.  
  76. /* Make a packed array.  See the comment in packed.h about */
  77. /* ensuring that refs in mixed arrays are properly aligned. */
  78. int
  79. make_packed_array(const ref *elts, uint size, ref *paref,
  80.   const char *client_name)
  81. {    /* Check whether we can make a packed array. */
  82.     const ref *endp = elts + size;
  83.     const ref *pref = elts;
  84.     ushort *pbody, *pdest;
  85.     ushort *pshort;            /* points to start of */
  86.                     /* last run of short elements */
  87.     int atype;
  88.     uint local = alloc_current_local();
  89.     /* Allocate a maximum-size array first, */
  90.     /* shorten later if needed. */
  91.     pbody = (ushort *)alloc(size, sizeof(ref), client_name);
  92.     if ( pbody == 0 )
  93.         return_error(e_VMerror);
  94.     pshort = pdest = pbody;
  95.     for ( ; pref < endp; pref++ )
  96.        {    switch ( r_btype(pref) )    /* not r_type, opers are special */
  97.            {
  98.         case t_name:
  99.             if ( name_index(pref) >= packed_max_name_index )
  100.                 break;    /* can't pack */
  101.             *pdest = name_index(pref) +
  102.               (r_has_attr(pref, a_executable) ?
  103.                pt_tag(pt_executable_name) :
  104.                pt_tag(pt_literal_name));
  105.             pdest++;
  106.             continue;
  107.         case t_integer:
  108.             if ( pref->value.intval < packed_min_intval ||
  109.                  pref->value.intval > packed_max_intval
  110.                )
  111.                 break;
  112.             *pdest = pt_tag(pt_integer) +
  113.               ((short)pref->value.intval - packed_min_intval);
  114.             pdest++;
  115.             continue;
  116.         case t_oparray:
  117.         case t_operator:
  118.            {    uint oidx;
  119.             if ( !r_has_attr(pref, a_executable) ) break;
  120.             oidx = op_index(pref);
  121.             if ( oidx == 0 || oidx > packed_int_mask ) break;
  122.             *pdest = pt_tag(pt_executable_operator) + oidx;
  123.            }    pdest++;
  124.             continue;
  125.         default:
  126.             /* Check for local-into-global store. */
  127.             if ( !local && r_is_local(pref) )
  128.             {    alloc_free((char *)pbody, size, sizeof(ref),
  129.                        client_name);
  130.                 return_error(e_invalidaccess);
  131.             }
  132.            }
  133.         /* Can't pack this element, use a full ref. */
  134.         /* We may have to unpack up to 3 preceding short elements. */
  135.            {    int i = (pdest - pshort) & 3;
  136.             ref *pnext = (ref *)(pdest + (packed_per_ref - 1) * i);
  137.             ref *pnext1 = pnext;
  138.             ref temp;    /* source & dest might overlap */
  139.             while ( --i >= 0 )
  140.                {    packed_get(--pdest, &temp);
  141.                 --pnext;
  142.                 ref_assign(pnext, &temp);
  143.                }
  144.             pdest = (ushort *)pnext1;
  145.             ref_assign(&temp, pref);
  146.             ref_assign_new(pnext1, &temp);
  147.            }
  148.         pdest += packed_per_ref;
  149.         pshort = pdest;
  150.        }
  151.     atype = (pdest == pbody + size ? t_shortarray : t_mixedarray);
  152.     pbody = (ushort *)alloc_shrink((byte *)pbody, size * packed_per_ref,
  153.                        (uint)(pdest - pbody), sizeof(ushort),
  154.                        client_name);
  155.     make_tasv_new(paref, atype, a_readonly | local, size, packed, pbody);
  156.     return 0;
  157. }
  158.  
  159. /* Get an element from a packed array. */
  160. /* (This works for ordinary arrays too.) */
  161. void
  162. packed_get(const ref_packed *packed, ref *pref)
  163. {    ref_packed elt = *packed;
  164.     switch ( elt >> packed_type_shift )
  165.        {
  166.     case pt_executable_operator:
  167.         op_index_ref(elt & packed_int_mask, pref);
  168.         break;
  169.     case pt_integer:
  170.         make_int(pref, (int)(elt & packed_int_mask) + packed_min_intval);
  171.         break;
  172.     case pt_literal_name:
  173.     case pt_literal_name + 1:
  174.         name_index_ref(elt & packed_max_name_index, pref);
  175.         break;
  176.     case pt_executable_name:
  177.     case pt_executable_name + 1:
  178.         name_index_ref(elt & packed_max_name_index, pref);
  179.         r_set_attrs(pref, a_executable);
  180.         break;
  181.     default:            /* (shouldn't happen) */
  182.     case pt_full_ref:
  183.         ref_assign(pref, (const ref *)packed);
  184.        }
  185. }
  186.  
  187. /* ------ Initialization procedure ------ */
  188.  
  189. op_def zpacked_op_defs[] = {
  190.     {"0currentpacking", zcurrentpacking},
  191.     {"1packedarray", zpackedarray},
  192.     {"1setpacking", zsetpacking},
  193.     op_def_end(0)
  194. };
  195.